@echo off
chcp 65001 >nul
echo ========================================
echo 下载并安装 Android SDK 工具
echo ========================================
echo.

REM 创建临时目录
set "TEMP_DIR=%TEMP%\android_sdk_tools"
if not exist "%TEMP_DIR%" mkdir "%TEMP_DIR%"

echo 正在下载 Android SDK Command-line Tools...
echo 下载地址：https://dl.google.com/android/repository/commandlinetools-win-latest.zip

REM 使用 PowerShell 下载文件
powershell -Command "& {[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri 'https://dl.google.com/android/repository/commandlinetools-win-latest.zip' -OutFile '%TEMP_DIR%\commandlinetools-win-latest.zip'}"

if not exist "%TEMP_DIR%\commandlinetools-win-latest.zip" (
    echo 下载失败，请手动下载：
    echo https://dl.google.com/android/repository/commandlinetools-win-latest.zip
    echo.
    echo 下载后解压到：C:\Android\Sdk\cmdline-tools\latest\
    pause
    exit /b 1
)

echo 下载完成，正在解压...
powershell -Command "Expand-Archive -Path '%TEMP_DIR%\commandlinetools-win-latest.zip' -DestinationPath '%TEMP_DIR%' -Force"

REM 创建目标目录
set "SDK_PATH=C:\Android\Sdk"
if not exist "%SDK_PATH%" mkdir "%SDK_PATH%"
if not exist "%SDK_PATH%\cmdline-tools" mkdir "%SDK_PATH%\cmdline-tools"
if not exist "%SDK_PATH%\cmdline-tools\latest" mkdir "%SDK_PATH%\cmdline-tools\latest"

REM 复制文件
echo 正在安装 SDK 工具...
xcopy "%TEMP_DIR%\cmdline-tools\*" "%SDK_PATH%\cmdline-tools\latest\" /E /I /Y

REM 设置环境变量
setx ANDROID_SDK_ROOT "%SDK_PATH%"
setx ANDROID_HOME "%SDK_PATH%"

echo.
echo SDK 工具安装完成！
echo 环境变量已设置：
echo ANDROID_SDK_ROOT=%SDK_PATH%
echo ANDROID_HOME=%SDK_PATH%
echo.
echo 请重新打开命令提示符或 PowerShell，然后运行：
echo %SDK_PATH%\cmdline-tools\latest\bin\sdkmanager.bat --list
echo.
pause 